package com.xpgk.xpsoft.common;

import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

//系统运行状态监控操作
public class XPSoftRunState extends Thread {
	// 阻塞队列，FIFO
	private static LinkedBlockingQueue<Record> m_LinkedQueue = new LinkedBlockingQueue<Record>();
	private static Thread m_thread = null;
	private static boolean bStop = false;

	// 初始化，启动日志写线程
	static public void init() {
		bStop = false;
		m_LinkedQueue.clear();
		m_thread = new XPSoftRunState();
		m_thread.start();
		System.out.println("run state thread start.");
	}

	static public void stop2() {
		bStop = true;
	}

	public static boolean write(long num, long sumTime, long num50, Date time) {
		if (m_thread == null)
			init();
		Record e = new Record();
		e.set("num", num).set("sumTime", sumTime).set("num50", num50).set("gxsj", time);
		return m_LinkedQueue.offer(e);
	}

	public void run() {
		Record record;
		while (!bStop) {
			// 队列空时，指定阻塞时间后返回，不会一直阻塞
			try {
				record = m_LinkedQueue.poll(5000, TimeUnit.MILLISECONDS);
				if (record != null) {
					Db.save("frm_runState", record);
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("run state thread stop.");
	}
}
